/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * Category entity data model.  A category is the highest level of organization.
 * @author Adam
 * @version $Id: Category.java 26 2009-06-01 05:54:53Z a.ruggles $
 * 
 * Created on Feb 8, 2009 at 10:31:05 PM 
 */
@javax.persistence.Entity @Table(name = "sapien_categories")
@NamedQueries({
	@NamedQuery(name = "category.listAll", query = "FROM com.google.code.sapien.model.Category"),
	@NamedQuery(name = "category.getCount", query = "SELECT COUNT(c) FROM com.google.code.sapien.model.Category c")
})
public class Category implements Comparable<Category>, Serializable {
    /**
	 * Serial Version UID.
	 */
	private static final long serialVersionUID = 6067751564659561556L;

	/**
     * Access control list for this category.  Contains permissions associated with various roles to this category.
     */
    private Set<CategoryAcl> acl = new HashSet<CategoryAcl>();

	/**
     * The description of the category.
     */
    private String description;

    /**
     * The number of discussions under this category.
     */
    private Integer discussionCount;

	/**
     * Unique identifier.
     */
    private Long id;

	/**
     * The last discussion that was updated in this category.
     */
    private Discussion lastDiscussion;

    /**
     * The number of messages under this category.
     */
    private Integer messageCount;

    /**
     * The name for the category.
     */
    private String name;

    /**
     * The order value, used to order the category in relation to other categories.
     */
    private Integer order;

    /**
     * The version for optimistic concurrency control.
     */
    private Integer version;

	/**
	 * Adds the category ACL to this category.
	 * @param categoryAcl The category ACL to add.
	 */
	public void addAcl(final CategoryAcl categoryAcl) {
		categoryAcl.setCategory(this);
		acl.add(categoryAcl);
	}

	/**
	 * {@inheritDoc}
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(final Category comparison) {
		int value = -1;
		if (this.order != null && comparison != null && comparison.getOrder() != null) {
			value = this.order.compareTo(comparison.getOrder());
		}
		return value;
	}

	/**
	 * {@inheritDoc}
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof Category)) {
			return false;
		}
		final Category rhs = (Category) obj;
		return new EqualsBuilder()
			.append(id, rhs.getId())
			.append(name, rhs.getName())
			.isEquals();
	}

	/**
	 * Returns acl.
	 * @return the acl.
	 */
	@OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
	public Set<CategoryAcl> getAcl() {
		return acl;
	}

	/**
	 * Returns description.
	 * @return the description.
	 */
	@Lob
	public String getDescription() {
		return description;
	}

	/**
	 * Returns discussionCount.
	 * @return the discussionCount.
	 */
	@Basic(optional = false)
	public Integer getDiscussionCount() {
		return discussionCount;
	}

	/**
	 * Returns id.
	 * @return the id.
	 */
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	/**
	 * Returns lastDiscussion.
	 * @return the lastDiscussion.
	 */
    @ManyToOne(optional = true, fetch = FetchType.LAZY) @JoinColumn(name = "last_discussion_id")
	public Discussion getLastDiscussion() {
		return lastDiscussion;
	}

	/**
	 * Returns messageCount.
	 * @return the messageCount.
	 */
    @Basic(optional = false)
	public Integer getMessageCount() {
		return messageCount;
	}

	/**
	 * Returns name.
	 * @return the name.
	 */
	@Column(length = 100, nullable = false)
	public String getName() {
		return name;
	}

	/**
	 * Returns order.
	 * @return the order.
	 */
	@Column(name = "forum_order", nullable = false)
	public Integer getOrder() {
		return order;
	}

	/**
	 * Returns version.
	 * @return the version.
	 */
	@Version
	public Integer getVersion() {
		return version;
	}

	/**
	 * {@inheritDoc}
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(3, 37)
			.append(id)
			.append(name)
			.toHashCode();
	}

	/**
	 * Sets acl.
	 * @param acl the acl to set.
	 */
	public void setAcl(final Set<CategoryAcl> acl) {
		this.acl = acl;
	}

	/**
	 * Sets description.
	 * @param description the description to set.
	 */
	public void setDescription(final String description) {
		this.description = description;
	}

	/**
	 * Sets discussionCount.
	 * @param discussionCount the discussionCount to set.
	 */
	public void setDiscussionCount(final Integer discussionCount) {
		this.discussionCount = discussionCount;
	}

	/**
	 * Sets id.
	 * @param id the id to set.
	 */
	public void setId(final Long id) {
		this.id = id;
	}

	/**
	 * Sets lastDiscussion.
	 * @param lastDiscussion the lastDiscussion to set.
	 */
	public void setLastDiscussion(final Discussion lastDiscussion) {
		this.lastDiscussion = lastDiscussion;
	}

	/**
	 * Sets messageCount.
	 * @param messageCount the messageCount to set.
	 */
	public void setMessageCount(final Integer messageCount) {
		this.messageCount = messageCount;
	}

	/**
	 * Sets name.
	 * @param name the name to set.
	 */
	public void setName(final String name) {
		if (name != null && name.length() > 100) {
			this.name = name.substring(0, 100);
		} else {
			this.name = name;
		}
	}

	/**
	 * Sets order.
	 * @param order the order to set.
	 */
	public void setOrder(final Integer order) {
		this.order = order;
	}

	/**
	 * Sets version.
	 * @param version the version to set.
	 */
	public void setVersion(final Integer version) {
		this.version = version;
	}

	/**
	 * Returns a JSON object representing this object in JavaScript.
	 * @return A String.
	 */
	public String toJSON() {
		return new StringBuilder("{")
			.append("id:").append(id)
			.append(",name:'").append(StringEscapeUtils.escapeJavaScript(name))
			.append("',description:'").append(StringEscapeUtils.escapeJavaScript(description))
			.append("',order:").append(order)
			.append(",discussions:").append(discussionCount)
			.append(",messages:").append(messageCount)
			.append(",version:").append(version)
			.append('}').toString();
	}

	/**
	 * {@inheritDoc}
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this)
			.append("id", id)
			.append("name", name)
			.append("description", description)
			.append("order", order)
			.append("discussions", discussionCount)
			.append("messages", messageCount)
			.append("version", version)
			.toString();
	}
}
